package com.huluobo.a2207douyindemo.shop;

import android.util.Log;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2207douyindemo.R;
import com.huluobo.a2207douyindemo.adapter.CartAdapter;
import com.huluobo.a2207douyindemo.databinding.ActivityCartBinding;
import com.huluobo.base.bean.CartBeanResult;
import com.huluobo.base.mvvm.BaseMVVMActivity;

public class CartActivity extends BaseMVVMActivity<ActivityCartBinding, ShopViewModel> {

    private CartAdapter cartAdapter;
    private CartBeanResult.CartBean cartBean;

    @Override
    protected void initData() {
        vm.getCartList();

        vm.getCartListSuccess.observe(this, cartBeanResult -> {
            if (cartBeanResult.getCode() == 200) {
                cartAdapter.getData().clear();
                cartAdapter.getData().addAll(cartBeanResult.getData());
                cartAdapter.notifyDataSetChanged();

                int totalPrice = 0;
                for (int i = 0; i < cartAdapter.getData().size(); i++) {
                    totalPrice += cartAdapter.getData().get(i).getGoods_default_price() * cartAdapter.getData().get(i).getCount();
                }
                binding.cartTotalPrice.setText("总价:" + totalPrice + "元");
            } else {
                ToastUtils.showLong(cartBeanResult.getMessage());
            }
        });

        vm.getCartListFailed.observe(this, s -> {
            Log.i("TagA", "获取购物车列表失败:" + s);
        });

        vm.deleteCartSuccess.observe(this, commonBeanResult -> {
            ToastUtils.showLong(commonBeanResult.getMessage());
            if (commonBeanResult.getCode() == 200) {
                cartAdapter.remove(cartBean);
                int totalPrice = 0;
                for (int i = 0; i < cartAdapter.getData().size(); i++) {
                    totalPrice += cartAdapter.getData().get(i).getGoods_default_price() * cartAdapter.getData().get(i).getCount();
                }
                binding.cartTotalPrice.setText("总价:" + totalPrice + "元");
            }
        });

        vm.deleteCartFailed.observe(this, s -> {
            Log.i("TagA", "删除购物车失败:" + s);
        });
    }

    @Override
    protected void initView() {
        binding.cartRv.setLayoutManager(new LinearLayoutManager(this));
        cartAdapter = new CartAdapter();
        binding.cartRv.setAdapter(cartAdapter);

        cartAdapter.addChildClickViewIds(R.id.item_cart_delete_iv);
        cartAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.item_cart_delete_iv) {
                vm.deleteCart(String.valueOf(cartAdapter.getData().get(position).getId()));
                cartBean = cartAdapter.getData().get(position);
            }
        });
    }

    @Override
    protected Class<ShopViewModel> getViewModel() {
        return ShopViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_cart;
    }
}